package ar.com.solidussource.howmuchservices.utils.json;

import java.io.IOException;
import java.util.List;

import ar.com.solidussource.howmuchservices.common.exception.JSONTranslationException;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JSONUtils {
	private static String EMPTY_LIST_RESULT = "[]";
	private static String EMPTY_RESULT = "{}";
	//private static String EMPTY_RESULT = "{\"status\":\"empty_result\"}";
	
	public static String getJSON(Object object) throws JSONTranslationException {
				
		try {
			if(object != null){
				
				if(object instanceof List<?>){
					List<?> list = (List<?>) object;
					if(list.isEmpty()) return EMPTY_LIST_RESULT;
				}
				
				ObjectMapper objectMapper = new HibernateAwareObjectMapper();				
				return objectMapper.writeValueAsString(object);
			}
			else return EMPTY_RESULT;			
			
		} catch (JsonGenerationException e) {
			throw new JSONTranslationException(e.getMessage());
		} catch (JsonMappingException e) {
			throw new JSONTranslationException(e.getMessage());
		} catch (IOException e) {
			throw new JSONTranslationException(e.getMessage());
		}	
	}

}
